package db

import (
	"MessageSendService/app/common/models/accounts"
	"MessageSendService/app/job/etc"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
)

var Db *gorm.DB

func MysqlConnectInit() {
	// 日志打印
	newLogger := logger.Default

	fmt.Println("数据库URL:", etc.Config.MySqlURL)
	v, err := gorm.Open(mysql.Open(etc.Config.MySqlURL), &gorm.Config{
		Logger: newLogger,
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true, // true不在表后面+ s，
		},
	})
	if err != nil {
		fmt.Println("Mysql:初始化失败!", err)
		panic(err)
	}
	Db = v
}

// GetAccountAll
// 获取所有账号信息
func GetAccountAll() []accounts.SendAccount {
	var accountList []accounts.SendAccount
	err := Db.Find(&accountList).Error
	if err != nil {
		fmt.Println("查询账号失败", err)
		panic("查询账号失败" + err.Error())
	} else {
		fmt.Println("查询账号成功", accountList)
	}
	return accountList
}
